var deviceone = require("deviceone");
var page = sm("do_Page");
var app = sm("do_App");
var storage = sm("do_InitData");
var external = sm("do_External");
var nf = sm("do_Notification");
var httpHelper = require("helper/http");
var httpSilence = require("helper/httpSilence");
var qqCacher = require("helper/qqCacher");

var rootview = ui("$");
var buttonA = mm("do_Animation", "BUTTONTOUCHDOWNS", "app");
var param = {oneCategory:[],oneSelected:{}};

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(300,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:260};
animPanelShow.append(300,propsPS,"Linear");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(300,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(300,propsPH,"EaseIn");

var upgradeDataJson = page.getData();
var needUpdContent = upgradeDataJson.needUpdContent;
var updFile = upgradeDataJson.updFile;
var updVersion = upgradeDataJson.updVersion;

var isUpdContentOnly = upgradeDataJson.isUpdContentOnly;
var needMustMarket = upgradeDataJson.needMustMarket;
var needMarket = upgradeDataJson.needMarket;
var marketFile = upgradeDataJson.marketFile;
var title = upgradeDataJson.title;
var contentList = upgradeDataJson.contentList;
qqCacher.upgradeDataJsonStr(JSON.stringify(upgradeDataJson));

qqCacher.upgradePageIsOpen("1");
function closeThePage(){
	qqCacher.upgradePageIsOpen("2");
	app.closePage();
}
page.on("loaded", function() {
	if(needMustMarket==false&&needUpdContent==true&&(needMarket==false||isUpdContentOnly==1)){
		buttonSure.text = "点击更新";
	}else{
		buttonSure.text = "前往下载";
	}
	uploadBoxList(contentList);
	uploadBox(true);
});
//遮罩事件
var maskbg = ui("bgmask");
maskbg.on("touch","",300,function(){});
//防止穿透
var bodyp = ui("pbody");
bodyp.on("touch","",300,function(){});
//确定  
var buttonSure = ui("buttonSure");
buttonSure.on("touch","",300,function(){
	qqCacher.upgradeDataJsonStr("");
	if(needMustMarket==false&&needUpdContent==true&&(needMarket==false||isUpdContentOnly==1)){
		updateZipContent();
	}else{
		if(needUpdContent==true){
			app.fire("fireDownloadNUpdateSilence",{"updFile":updFile,"updVersion":updVersion});
		}
		external.openURL(marketFile);
		closeThePage();
	}
});
function updateZipContent(){
	buttonSure.text = "更新中..";
	var upgradeHelper = require("helper/upgrade");
	buttonSure.enabled = false;
	var targetFile = upgradeHelper.appUpdateNewUri();
	httpHelper.download(updFile,targetFile,function(data){
		buttonSure.enabled = false;
		buttonCancle.enabled = false;
		buttonSure.text = "安装中..";
		upgradeHelper.updateContent(updVersion,function(){
			nf.toast("升级成功，请重新打开app");
			buttonSure.text = "更新成功!";
			maskbg.animate(animMaskHide,function(){
				maskbg.visible = false;
			});
			bodyp.animate(animPanelHide);
			closeThePage();
		});
	},function(data){
		buttonSure.enabled = true;
		buttonCancle.enabled = true;
		buttonSure.text = "请再点击更新";
	},function(data){
		buttonSure.enabled = false;
		buttonCancle.enabled = false;
		buttonSure.text = parseInt(data.currentSize*100/data.totalSize)+"%";
	});
}

var buttonCancle = ui("buttonCancle");
buttonCancle.on("touch","",300,function(){
	qqCacher.upgradeDataJsonStr("");
	if(needUpdContent==true){
		app.fire("fireDownloadNUpdateSilence",{"updFile":updFile,"updVersion":updVersion});
	}
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
	bodyp.animate(animPanelHide);
	closeThePage();
});
//zdy
function uploadBox(data){
	maskbg.visible = data;
	maskbg.animate(animMaskShow);
	bodyp.animate(animPanelShow);
}
var uploadList = ui("uploadList");
var uploadData = mm("do_ListData");
function uploadBoxList(data){
	uploadData.removeAll();
	uploadList.bindItems(uploadData);
	uploadData.addData(data);
	uploadList.refreshItems();
}